package com.tjsinfo.tjsoa.addresslist.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.addresslist.bo.IAddressListUserBo;
import com.tjsinfo.tjsoa.addresslist.bo.IAddressListUserPageBo;
import com.tjsinfo.tjsoa.addresslist.dao.IAddressListGroupDAO;
import com.tjsinfo.tjsoa.addresslist.vo.AddressListGroup;
import com.tjsinfo.tjsoa.addresslist.vo.AddressListUser;
import com.tjsinfo.tjsoa.system.bo.ISystemUserBo;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;
import com.tjsinfo.tjsoa.system.vo.User;

public class InsertAddressListUserAction extends Action{
	private IAddressListGroupDAO listGroupDAO;
	private IAddressListUserBo userBo;
	private ISystemUserBo tjsUserBo;
	private IAddressListUserPageBo listUserPageBo;

	private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	private Date date = null;
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		String groupid = request.getParameter("groupid");
		String yuangroupid = request.getParameter("yuangroupid");
		String moduleType = request.getParameter("moduleType");
		
		String userName = request.getParameter("userName");
		String userSort = request.getParameter("userSort");
		String mobile = request.getParameter("mobile");
		String email = request.getParameter("email");
		
		String homeAddress = request.getParameter("homeAddress");
		String homePhone = request.getParameter("homePhone");
		String homePostal = request.getParameter("homePostal");
		
		String company = request.getParameter("company");
		String sector = request.getParameter("sector");
		String duty = request.getParameter("duty");
		String companyPhone = request.getParameter("companyPhone");
		String companyFax = request.getParameter("companyFax");
		String companyAddress = request.getParameter("companyAddress");
		String companyPostal = request.getParameter("companyPostal");
		
		String birthday = request.getParameter("birthday");
		String qq = request.getParameter("qq");
		String msn = request.getParameter("msn");
		String blog = request.getParameter("blog");
		String remarks = request.getParameter("remarks");
		
		AddressListGroup group = listGroupDAO.findById(groupid);
		HttpSession session = request.getSession();
		User users = (User) session.getAttribute("user");
		TSystemUser tjsuser = tjsUserBo.findByIdSystemUser(users.getUser_id());
		
		AddressListUser listUser = new AddressListUser();
		listUser.setAddressListGroup(group);
		listUser.setTjsUser(tjsuser);
		if (moduleType.equals("0")) {
			if (!yuangroupid.equals(groupid)) {
				userSort = userBo.getMaxUserSort(groupid);
			}
			if (userSort != null && !"".equals(userSort)) {
				listUser.setUserSort(Integer.valueOf(userSort));
			}
		}
		listUser.setUserName(userName);
		listUser.setMobile(mobile);
		listUser.setEmail(email);
		listUser.setHomeAddress(homeAddress);
		listUser.setHomePhone(homePhone);
		listUser.setHomePostal(homePostal);
		listUser.setCompany(company);
		listUser.setSector(sector);
		listUser.setDuty(duty);
		listUser.setCompanyPhone(companyPhone);
		listUser.setCompanyFax(companyFax);
		listUser.setCompanyAddress(companyAddress);
		listUser.setCompanyPostal(companyPostal);
		if (birthday != null && !"".equals(birthday)) {
			try {
				date = sdf.parse(birthday);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			listUser.setBirthday(date);
		}
		listUser.setQq(qq);
		listUser.setMsn(msn);
		listUser.setBlog(blog);
		listUser.setState(Byte.valueOf("1"));
		listUser.setCreateTime(new Date());
		if (remarks != null && !"".equals(remarks)) {
			remarks = remarks.replace("\"", "“");
			listUser.setRemarks(remarks.trim());
		}
		String user_id = userBo.saveAddressListUser(listUser);
		
		int j = listUserPageBo.getPageCount(groupid,String.valueOf(17));
		String page = Integer.valueOf(j).toString(); 
		request.setAttribute("group_id", groupid);
		request.setAttribute("first", "insert");
		request.setAttribute("user_id", user_id);
		request.setAttribute("user_page", page);
		request.setAttribute("moduleType", moduleType);
		return mapping.findForward("ok");
	}
	public void setListGroupDAO(IAddressListGroupDAO listGroupDAO) {
		this.listGroupDAO = listGroupDAO;
	}
	public void setUserBo(IAddressListUserBo userBo) {
		this.userBo = userBo;
	}
	public void setTjsUserBo(ISystemUserBo tjsUserBo) {
		this.tjsUserBo = tjsUserBo;
	}
	public void setListUserPageBo(IAddressListUserPageBo listUserPageBo) {
		this.listUserPageBo = listUserPageBo;
	}
	public void setSdf(SimpleDateFormat sdf) {
		this.sdf = sdf;
	}
	public void setDate(Date date) {
		this.date = date;
	}
}
